/**
 * Created by root on 16-12-28.
 */

function menu_hander(data) {
    var data = JSON.parse(data);
            $('#side-menu').empty()
            $('#side-menu').append('<li class="nav-header">'+
                                        '<div class="dropdown profile-element">'+
                                            '<a data-toggle="dropdown" class="dropdown-toggle" href="#">'+
                                                '<span class="clear">'+
                                                    '<span class="block m-t-xs" style="font-size:20px;">'+
                                                        '<i class="fa fa-area-chart"></i>'+
                                                        '<strong class="font-bold">运维管理平台</strong>'+
                                                    '</span>'+
                                                '</span>'+
                                            '</a>'+
                                        '</div>'+
                                        '<div class="logo-element">frbao-omp'+
                                        '</div>'+
                                    '</li>')

            for(menu in data){
                $("#side-menu").append('<li id="item_'+menu+'">' +
                                        '<a onclick="menu_on(this)">'+
                                            '<i class="fa fa fa-bar-chart-o"></i>'+
                                            '<span class="nav-label">'+menu+'</span>'+
                                            '<span class="fa arrow"></span>'+
                                        '</a>' +
                                    '</li>')
                data[menu].forEach(function (item) {
                    $("#item_"+menu).append('<ul id="ul_'+menu+'" class="nav nav-second-level collapse" aria-expanded="false">'+
                            '<li>'+
                                '<a class="J_menuItem" href="'+item['item_link']+'">'+item['item_name']+'</a>'+
                            '</li>'+
                        '</ul>')})
            }
}


jQuery(function () {

    $("#nav-header-top").append('<li class="dropdown">'+
                                '<a class="dropdown-toggle count-info" data-toggle="dropdown" href="#">' +
                                    '<i class="fa fa-bell">主页</i>'+
                                '</a>'+
                            '</li>')
    $.get({
        'url':'/nav/configNav/getNavHeader',
        'success':function (callback) {
            var navData = JSON.parse(callback);
            navData.forEach(function (name) {
                $("#nav-header-top").append('<li class="dropdown">'+
                                '<a class="dropdown-toggle count-info" data-toggle="dropdown" onclick="getMenu(this)">' +
                                    '<i class="fa">'+name+'</i>'+
                                '</a>'+
                            '</li>')
            });
        }
    });

    var data = $.cookie('item');
    menu_hander(data);
})




function getMenu(this_id) {
    nav_name = $(this_id).text();
    $.get({
        'url':'/nav/getItem/',
        'data':{'nav_name':nav_name},
        'success':function (callback) {
            $.cookie('item',callback,{ expires:7, path:'/'});
            menu_hander(callback);
        }
    })
}

//点击菜单,展开菜单项
function menu_on(this_id) {
    var flag = $(this_id).text();
    if($("#ul_"+flag).hasClass('in')){
        $("#ul_"+flag).removeClass('in');
        $("#item_"+flag).removeClass('active');
    }else{
        $("#ul_"+flag).addClass('in');
        $("#item_"+flag).addClass('active');
    }
}